<?php
class Mdbmeter extends MY_Controller {

                function __construct() {
                    parent::__construct();
					
                            $this->load->model('master/metermaster_model');
                            $this->load->model('electric_meter/mdbmeter_model');
                            $this->load->model('electric_meter/mainmeter_model');

                            $this->load->model('master/corporate_model');
                            $this->load->model('grid_model');

                            $this->load->helper('common');
					
                }
                function index($offset = 0)
                {
					
                        if ($this->input->post('user_action')=="approve") $this->approve();
						else if ($this->input->post('user_action')=="update_remark") $this->update_remark();
                        else
                            {
                                   if($this->input->post('fccode'))
                                   {

                                            $fccode=$this->input->post('fccode');
                                            $building=$this->mdbmeter_model->get_building($fccode);
                                            $bdcode=$this->input->post('bdcode');
                                            if (!$bdcode){
                                                     $building=$this->mdbmeter_model->get_building($fccode);
                                                     $bdcode=$building[0]["BDCODE"];
                                            }
                                            $year=$this->input->post('year');
                                            $month=$this->input->post('month');
                                            $date=$this->input->post('date');
                                    }
                                    else
                                    {
                                            $today = getdate();
                                            $month = $today['mon'];
                                            $year = $today['year'];
                                            $fccode=$this->session->userdata('corporate')->FCCODE;
                                            $building=$this->mdbmeter_model->get_building($fccode);
                                            $bdcode=$building[0]["BDCODE"];
                                            $date='';
                                    }
                                    $this->view($fccode,$bdcode,$year,$month,$date);
                            }
							
						
                }

 		function add()
 		{

				//$this->mdbmeter_model->delete($fccode,$date,$mtcode);
			//print_r($this->input->post('rmu'));
			$date=$this->input->post('date');
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$mmcode=$this->input->post('mmcode');
			$arr=explode( '-' , $date ); //
			$year=$arr[2];
			$month=$arr[0];
			//$this->mdbmeter_model->delete($fccode,$bdcode,"2",$date);
			for($i=0;$i<count($mmcode);$i++)
			{
				$this->mdbmeter_model->delete_by_meter($mmcode[$i],$date);
				$unit=$this->input->post('unit');
				$last=$this->input->post('mdb_last');

				$meter=$this->metermaster_model->get_by_id($mmcode[$i]);

                                if  ($meter->ISDECIMAL=="Y")
                                    if ($unit[$i]<$last[$i]) $meter_diff=($meter->MAX_UNIT-$last[$i])+$unit[$i]+0.1;
                                    else  $meter_diff=$unit[$i]-$last[$i];
                                else 
                                    if ($unit[$i]<$last[$i]) $meter_diff=($meter->MAX_UNIT-$last[$i])+$unit[$i]+1;
                                    else  $meter_diff=$unit[$i]-$last[$i];

				$items = array('mmcode' =>$meter->MMCODE,
						   	   'mrcode' => $meter->MRCODE,
							   'entry_year' =>$year,
							   'entry_month' => $month,
							   'entry_week' =>getWeekOfMonth($date),
							   'entry_date' => $date,
							   'entry_time'=>$this->input->post('entry_time'),
							   'meter_unit' =>$unit[$i],
							   'meter_diff'=>$meter_diff,
							   'fix_rate' => $meter->FIX_RATE,
							   'user_modify' => $this->session->userdata('username'),
							   'date_modify' => date('Y-m-d H:i:s')
				);
				$this->mdbmeter_model->save($items);
				$this->mdbmeter_model->update($meter->MMCODE,$unit[$i],$date);
			}
		   $next_date=$this->mainmeter_model->get_next_date($date);
		   $arr=explode( '-' , $next_date->NEXT_DATE ); //
		   $this->view($fccode,$bdcode,(int)$arr[2],(int)$arr[0],$next_date->NEXT_DATE);

 		}

 		function delete()
 		{
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$year=$this->input->post('year');
			$month=$this->input->post('month');
			$date=$this->input->post('date');
			$mmcode=$this->input->post('mmcode');
			for($i=0;$i<count($mmcode);$i++)
			{
				$this->mdbmeter_model->delete_by_meter($mmcode[$i],$date);
			}
            $this->view($fccode,$bdcode,$year,$month,'');
 		}
        function approve()
 		{
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$year=$this->input->post('year');
			$month=$this->input->post('month');
			$date=$this->input->post('date');
			$mmcode=$this->input->post('mmcode');
			$this->mdbmeter_model->approve($fccode,$bdcode,$date,$this->session->userdata('username'));
            $this->view($fccode,$bdcode,$year,$month,'');
 		}
        function update_remark()
 		{
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$year=$this->input->post('year');
			$month=$this->input->post('month');
			$date=$this->input->post('date');
			$mmcode=$this->input->post('mmcode');
			$remark=  $this->input->post('remark');		
  
			foreach($remark as $k=>$value)
			{
			     $this->mdbmeter_model->update_remark($k,$date,$value);
			}
 
            $this->view($fccode,$bdcode,$year,$month,'');
 
 		}
		function get_json_remark()
		{
			//$arr = array(0=>'Root', 1=>'27') ;
			header("Content-Type: text/html; charset=tis-620");
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$date=$this->input->post('date');
			$arr=$this->mdbmeter_model->get_json_remark($fccode,$bdcode,$date);
   		    echo json_encode( $arr );

		}
  		function view($fccode,$bdcode,$year,$month,$date)
 		{
			
			$data['month'] =echo_month("month",$month);
			$data['year'] =echo_year("year",$year);
			$data['date']=$date;
			$corporate=$this->corporate_model->get_corp_user($this->session->userdata('username'));
			$building=$this->mdbmeter_model->get_building($fccode);
			$data['fccode'] =echo_dropdown_list("fccode",$corporate,"FCCODE","FCNAME",$fccode,false,"style='width:370px'  onchange='change_corporate()'");
			$data['bdcode']=$bdcode;
			$data['building']=echo_tab_list("bdcode",$building,"BDCODE","BDNAME",$bdcode);
			$data['title']= $this->my_controller->name;
			$data['list_remark']=$this->mdbmeter_model->gen_remark($fccode,$bdcode);
			$data['action'] = site_url($this->my_controller->controller);
			$data['table']=$this->mdbmeter_model->generate_meter($fccode,$bdcode,$year,$month,$date,$this->my_controller->can_approve);
			
			$this->template->write('title',$data['title']);
			$this->template->write_view('content', 'electric_meter/mdbmeter_list',$data);
			$this->template->render();
 		}
	 
}
?>